package com.serve.oa.controller;


import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.serve.oa.common.lang.Result;
import com.serve.oa.entity.ComOrganization;
import com.serve.oa.entity.SysMenu;
import com.serve.oa.entity.SysUser;
import com.serve.oa.entity.comUser;
import com.serve.oa.service.ComOrganizationService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author __tyw
 * @since 2022-12-06
 */
@RestController
@RequestMapping("/organization")
public class ComOrganizationController extends BaseController {

    @Autowired
    ComOrganizationService comOrganizationService;

    @GetMapping("list")
    @RequiresPermissions("com:organization:list")
    public Result list (){


        //配置
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
// 自定义属性名 都要默认值的
        treeNodeConfig.setNameKey("name");
        treeNodeConfig.setIdKey("id");
        treeNodeConfig.setParentIdKey("parentId");
        treeNodeConfig.setChildrenKey("children");


//转换器

        List<ComOrganization> menus = comOrganizationService.list();
        List<Tree<String>> treeNodes = TreeUtil.build(menus, "0",treeNodeConfig,(treeNode, tree)->{
            tree.setId(treeNode.getId().toString());
            tree.setParentId(treeNode.getParentId().toString());
            tree.setName(treeNode.getName());
        });



        //List<SysMenu> menus = menuService.tree();
        return Result.succ(treeNodes);


    }


    @GetMapping("/{id}")
    @RequiresPermissions("com:organization:list")
    public Result getMenuById (@PathVariable("id") Long id){

        Assert.notNull(id, "id必传");
        List<comUser> list = comOrganizationService.getOrgeUser(id);


        return Result.succ(list);

    }

    @PostMapping("/eidt/{id}")
    @RequiresPermissions("com:organization:eidt")
    public Result eidtMenuByIds (@PathVariable("id") Long id,@RequestBody List<Long> params){


        Assert.notNull(id, "id必传");

        String list = comOrganizationService.eidtOrgeUser(id,params);


        return Result.succ(list);

    }


    @GetMapping("/userList")

    @RequiresPermissions("com:organization:list")
    public Result GetuserList (){

        List<SysUser> list = userService.list(new QueryWrapper<SysUser>().eq("statu",1));


        return Result.succ(list);

    }

    @PostMapping("/setMan")
    @RequiresPermissions("com:organization:eidt")
    public Result setMan (@RequestBody Map<String,Long> obj){

        int list = comOrganizationService.setMan(obj);


        return Result.succ("ok");

    }

    @DeleteMapping("man/{id}")
    @RequiresPermissions("com:organization:eidt")
    public Result deleteOrder(@PathVariable("id") Long id){
        comOrganizationService.delman(id);
        return Result.succ("ok");
    }




}
